package com.project2.controller;

import com.project2.bean.ResultVo;
import com.project2.bean.bo.CategoryAddBo;
import com.project2.bean.bo.CategoryUpdateBo;
import com.project2.bean.bo.ChildrenInCategoryUpdateBo;
import com.project2.bean.vo.DataInAdminCategoryVo;
import com.project2.bean.vo.DataInCategoryAddVo;
import com.project2.bean.vo.DataInCategoryL1Vo;
import com.project2.service.AdminCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangminghui
 * @date 2022/8/13
 */
@RestController
@RequestMapping("admin/category")
public class AdminCategoryController {
    @Autowired
    AdminCategoryService adminCategoryService;

    @RequiresPermissions("admin:category:list")
    @RequestMapping("list")
    public ResultVo list() {
        DataInAdminCategoryVo data = adminCategoryService.queryAllCategoryInfo();
        return ResultVo.ok(data);
    }

    @RequiresPermissions("admin:category:create")
    @RequestMapping("create")
    public ResultVo create(@RequestBody CategoryAddBo categoryAddBo) {
        adminCategoryService.addCategoryByBo(categoryAddBo);
        DataInCategoryAddVo data = adminCategoryService.queryNewAddCategoryInfoByNameAndDesc(
                categoryAddBo.getName(), categoryAddBo.getDesc());
        return ResultVo.ok(data);
    }

    @RequiresPermissions("admin:category:update")
    @RequestMapping("update")
    public ResultVo update(@RequestBody CategoryUpdateBo categoryUpdateBo) {
        // todo: 如果请求携带的level为L1，那么可能父子目录都有更新
        //  如果请求携带的level为L2，那么只可能是子目录更新
        if (categoryUpdateBo.getLevel() != null && "L1".equals(categoryUpdateBo.getLevel())) {
            // todo: 先更新父目录，再更新子目录
            adminCategoryService.updateParentCategoryInfoByBo(categoryUpdateBo);
            // todo: 更新子目录，和下面的方法有一点点不一样，不能复用
            for (ChildrenInCategoryUpdateBo beanInChildren : categoryUpdateBo.getChildren()) {
                adminCategoryService.updateChildrenCategoryInfo(beanInChildren);
            }
        }
        if (categoryUpdateBo.getLevel() != null && "L2".equals(categoryUpdateBo.getLevel())) {
            adminCategoryService.updateChildCategoryInfoByBo(categoryUpdateBo);
        }
        return ResultVo.ok();
    }

    @RequestMapping("l1")
    public ResultVo l1() {
        DataInCategoryL1Vo data = adminCategoryService.queryAllL1LevelCategoryInfo();
        return ResultVo.ok(data);
    }

    @RequiresPermissions("admin:category:delete")
    @RequestMapping("delete")
    public ResultVo delete(@RequestBody CategoryUpdateBo categoryUpdateBo) {
        // todo: 如果请求携带的level为L1，那么可能父子目录都被删除
        //  如果请求携带的level为L2，那么只可能是子目录被删除
        //  可以复用update的bo，只有个别字段的差别，不影响
        if (categoryUpdateBo.getLevel() != null && "L1".equals(categoryUpdateBo.getLevel())) {
            // todo: 先删除子目录
            for (ChildrenInCategoryUpdateBo child : categoryUpdateBo.getChildren()) {
                adminCategoryService.deleteChildCategory(child.getId());
            }
            // todo: 后删除父目录
            adminCategoryService.deleteParentCategory(categoryUpdateBo.getId());
        }
        if (categoryUpdateBo.getLevel() != null && "L2".equals(categoryUpdateBo.getLevel())) {
            adminCategoryService.deleteChildCategory(categoryUpdateBo.getId());
        }
        return ResultVo.ok();
    }

}
